package com.morontestandroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class EighthLevel extends Activity implements SensorEventListener {
	
	//private float mLastX, mLastY, mLastZ;
	//private boolean mInitialized;
	private SensorManager mSensorManager;

	private Sensor mAccelerometer;
	float sensorX, sensorY, sensorZ;
	ImageView ball, goal;
	Chronometer mChronometer;
	//Bitmap ball2;
	//Paint paint;

	@Override

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eighth_level);

		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
		
		//ball2 = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
		mChronometer = (Chronometer) findViewById(R.id.level8ChrmId);
		ball = (ImageView)findViewById(R.id.ballId);
		goal = (ImageView)findViewById(R.id.goalId);
		
		mChronometer.setBase(getTime());          
		mChronometer.start();
		
		moveTheBall();
		
	       TextView txt = (TextView) findViewById(R.id.level1QuestionId);  
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        txt.setTypeface(font);
	}
	
	public long getTime() {
		  Bundle extras = getIntent().getExtras();
		  long time = extras.getLong("time");
		  return time;
		 
	}
	
	protected void onResume() {
		//is called when the Activity object and its views become interactive with the user
		super.onResume();
		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
		moveTheBall();
	}

	protected void onPause() {
		// is called when a different Activity instance is going to be visible 
		// and the current Activity has stopped interacting with the user
		super.onPause();
		mSensorManager.unregisterListener(this);
	}
	
	 @Override
	 protected void onStop() {
		 //is called when an activity is no longer visible to, or interacting with, the user
	     super.onStop();
	     mSensorManager.unregisterListener(this);
	     
	 }

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		//get the values of sensor.
		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
			sensorX = event.values[0];
			moveTheBall();
			//sensorY = event.values[1];
			//sensorZ = event.values[2];
			
		}
		
	}

	public void moveTheBall() {
		AbsoluteLayout.LayoutParams ballParams = (AbsoluteLayout.LayoutParams)ball.getLayoutParams();
		AbsoluteLayout.LayoutParams goalParams = (AbsoluteLayout.LayoutParams)goal.getLayoutParams();
		//LayoutParams position = (LayoutParams)ball.getLayoutParams();
		ballParams.x = 15 + (int)sensorX*55;
		ball.setLayoutParams(ballParams);
		
		if(ballParams.x >= goalParams.x) {
			startTenthLevel();
		}
		if(ballParams.x < 8) {
			startCheckPoint();
			
		}
		
	}
	
	public void startCheckPoint() {
		int activityID = 1;
		Intent intent;
		intent = new Intent().setClass(this, CheckPoint.class);
		intent.putExtra("time", getThisBase());
		startActivityForResult(intent, activityID);
		
	}
	
	public void startTenthLevel() {
		int activityID = 1;
		Intent intent;
		intent = new Intent().setClass(this, TenthLevel.class);
		intent.putExtra("time", getThisBase());
		startActivityForResult(intent, activityID);
		
	}
	
	public long getThisBase(){
		 return mChronometer.getBase();
	}
	public void onBackPressed() {
		// do something on back.
		startActivity(new Intent(this, MenyActivity.class));
		return;
		}
	
}